<?php

namespace app\common\model;

class ShopOrderRefundModel extends BaseModel
{
    protected $name = 'shop_order_refund'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    //退款方式
    public function getRefundTypeTextAttr($value, $data)
    {
        $state = [
            '1' => '退回到会员帐户',
            '2' => '银行转账、现金交付等',
            '3' => '原路退回',
        ];
        return $state[$data['refund_type']];
    }

    //处理状态
    public function getSellerStatusTextAttr($value, $data)
    {
        $state = [
            '-1' => '不同意',
            '0'  => '待审核',
            '1'  => '已同意',
        ];
        return $state[$data['seller_status']];
    }

    //处理状态
    public function getRefundStatusTextAttr($value, $data)
    {
        $state = [
            '1' => '处理中',
            '2' => '已完成',
        ];
        return $state[$data['refund_status']];
    }
    /**
     * 添加退款申请
     * @param $ordersn
     * @param $price
     * @param $buyer_message
     */
    public function addRefund($order_sn,$refund_amount,$buyer_message)
    {
        $orderModel      = new ShopOrderModel();
        $orderInfo       = $orderModel->where('order_sn', $order_sn)->find();
        if(empty($orderInfo)){
            $this->error='找不到指定订单，请重新操作';
            return false;
        }
        $data['order_id']=$orderInfo['order_id'];
        $data['order_sn']=$orderInfo['order_sn'];
        $data['site_id']=$orderInfo['site_id'];
        $data['buyer_uid']=$orderInfo['buyer_uid'];
        $data['buyer_username']=$orderInfo['buyer_username'];
        $data['refund_amount']=$refund_amount;
        $data['buyer_message']=$buyer_message;
        $data['create_time']=time();
        $info=$this->where('order_sn', $order_sn)->find();
        if(empty($info)){
            $this->addData($data);
            return true;
        }else{
            $this->error='您已经提交过退款申请，请等待审核！';
                return false;
        }


    }

}